﻿using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using Silk.NET.Vulkan;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;

namespace VkSmaple415
{
    public class GroupItemMiddleware : IProductionLineMiddleware<VulkanContext>
    {
        public void Invoke(VulkanContext context, RenderingProductionLineDelegate<VulkanContext> next)
        {
            var pipeline = context.VulkanPipeline;
            var vro = context.GetContextRenderObject().GetComponents<VRO>().First();
            var cmd = context.CommandBuffer;

            cmd.BindVertexBuffers(0, stackalloc ulong[] { 0 }, vro.VerticesBufferInfo.Buffer);

            ref MVPMatrix local = ref context.GetMVPMatrix();
            Matrix4x4 mat = local.ModelMatrix * local.ViewMatrix * local.ProjectionMatrix * context.ClipMatrix;
            byte[] data = mat.ToBytes();
            cmd.PushConstants(pipeline.PipelineLayout, ShaderStageFlags.ShaderStageVertexBit, 0, data);
            cmd.DrawIndirect(vro.DrawIndirectBufferInfo.Buffer, 0, (uint)vro.DrawIndirectCommands.Length, (uint)Marshal.SizeOf<DrawIndirectCommand>());
            next(context);
        }
    }
}
